package com.ebeitech.doorapp.view.homepage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebeitech.doorapp.R;
import com.ebeitech.doorapp.application.EbeiApplication;
import com.ebeitech.doorapp.constants.ActionConstants;
import com.ebeitech.doorapp.constants.CommonConstants;
import com.ebeitech.doorapp.http.HttpService;
import com.ebeitech.doorapp.util.PublicUtil;
import com.ebeitech.doorapp.view.BaseActivity;
import com.ebeitech.doorapp.view.homepage.mvppresenter.LoginPresenterModule;
import com.ebeitech.doorapp.view.homepage.mvpview.LoginContract;
import com.ebeitech.doorapp.view.homepage.mvpview.DaggerLoginComponent;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.ui.dialog.*;
import com.ebeitech.library.util.CommonUtil;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.ViewUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements LoginContract.View {


	@BindView(R.id.etUserName)
	EditText etUserName;
	@BindView(R.id.etPassword)
	EditText etPassword;
	@BindView(R.id.btnLogin)
	TextView btnLogin;
	@BindView(R.id.loginFrame)
	LinearLayout loginFrame;

	CommonAlertDialogFragment mProgressDialog;

	@Inject
	LoginContract.Presenter presenter;

	private boolean isHomeShowVersionDialog = false;

	private boolean isLogout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ebei_activity_login);
		ButterKnife.bind(this);
		DaggerLoginComponent.builder()
				.loginPresenterModule(new LoginPresenterModule(this))
				.build()
				.inject(this);

		if(!CommonUtil.isNetworkAvailable(this)){
			ViewUtil.toastMsg(this, R.string.ebei_network_disconnect);
		}

		Intent intent = getIntent();
		if (intent != null) {
			isLogout = intent.getBooleanExtra("isLogout",false);
		}

		initView();
		registerBroadCast();
	}

	@OnClick({R.id.btnLogin})
	void onClick(View view) {
		switch (view.getId()) {
			case R.id.btnLogin: {
				SPManager.getInstance(this).put("isDialogShowing", false);
				if(StringUtil.isStringNullOrEmpty(etUserName.getText().toString())){
					ViewUtil.toastMsg(this, R.string.ebei_login_account_null);
					return;
				}
				if(StringUtil.isStringNullOrEmpty(etPassword.getText().toString())){
					ViewUtil.toastMsg(this, R.string.ebei_login_password_null);
					return;
				}

				String mPassword = etPassword.getText().toString();
				presenter.doLogin(etUserName.getText().toString(), mPassword);
				break;
			}
		}
	}

	private void initView() {
		// 如果之前被挤下线,弹框提醒
		boolean isLoginOffline = SPManager.getInstance(this).get(CommonConstants.IS_LOGIN_OFFLINE, true);
		boolean isLoginOfflineRemind = SPManager.getInstance(this).get(CommonConstants.IS_LOGIN_OFFLINE_REMIND, true);
		if (!isLoginOfflineRemind) {
			SPManager.getInstance(this).put(CommonConstants.IS_LOGIN_OFFLINE_REMIND, true);
			final Context context = EbeiApplication.getContext();// 防止context不正确,DialogFragment报错
			CommonAlertDialogFragment dialog = ViewUtil.getAlertDialog(context, "", context.getString(R.string.login_with_another_client), new CommonAlertDialogFragment.OnBtnClickListener() {
				@Override
				public void onClick(View v, CommonAlertDialogFragment dialog) {
					ViewUtil.dismissDialog(dialog);
					PublicUtil.doLogout(context);
				}
			}, null, "", "");
			dialog.setCancelButtonVisible(false);
			ViewUtil.showDialog(context, dialog);
			loginFrame.setVisibility(View.VISIBLE);
		} else {
			if (isLogout || isLoginOffline) {
				String userAccount = SPManager.getInstance(EbeiApplication.getContext()).get(SPConstants.USER_ACCOUNT, "");
				showUserAccount(userAccount, "");
				showLoginFrame();
			} else {
				loginFrame.setVisibility(View.GONE);
				presenter.autoLogin(this);
			}
		}
	}

	@Override
	public void showProgress(String message) {
		mProgressDialog = ViewUtil.showProgressDialog(this);
	}

	@Override
	public void dismissProgress() {
		ViewUtil.dismissDialog(mProgressDialog);
	}

	@Override
	public void showLoginResult(String resultMessage) {
		if(!StringUtil.isStringNullOrEmpty(resultMessage)){
			ViewUtil.toastMsg(this,resultMessage);
		}
	}

	@Override
	public void loginSuccess() {
		SPManager.getInstance(this).put(CommonConstants.IS_LOGIN_OFFLINE, false);
		HttpService.getInstance().resetHttp();
		ViewUtil.toastMsg(this, R.string.ebei_login_success, 500);
		Intent intent = new Intent();
		intent.setClass(this, HomepageActivity.class);
		intent.putExtra("isHomeShowVersionDialog",isHomeShowVersionDialog);
		startActivity(intent);
		setResult(RESULT_OK, null);
		finish();
	}

	@Override
	public void setIsHomeShowVersionUpdateDialog(boolean flag) {
		isHomeShowVersionDialog = flag;
	}

	@Override
	public void showLoginFrame() {
		loginFrame.setVisibility(View.VISIBLE);
		if (!isLogout)
			PublicUtil.checkVersion(this);
	}

	@Override
	public void showUserAccount(String userAccount, String password) {
		etUserName.setText(StringUtil.getDefaultString(userAccount));
	}

	private void registerBroadCast() {
		IntentFilter myIntentFilter = new IntentFilter();
		myIntentFilter.addAction(ActionConstants.ACTION_EXIT_APP);
		registerReceiver(mBroadcastReceiver, myIntentFilter);
	}

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (ActionConstants.ACTION_EXIT_APP.equals(action)) {
				finish();
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				finish();
			}
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mBroadcastReceiver);
	}


}
